<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce">
							<el-col v-if="$check_field('get','enterprise_user') || $check_field('add','enterprise_user') || $check_field('set','enterprise_user')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="企业用户" prop="enterprise_user">
																		<div v-if="user_group !== '管理员'">
							{{ get_user_session_enterprise_user(form['enterprise_user']) }}
							<el-select v-if="(form['online_recruitment_id'] && $check_field('set','enterprise_user')) || (!form['online_recruitment_id'] && $check_field('add','enterprise_user'))" id="enterprise_user" v-model="form['enterprise_user']" :disabled="disabledObj['enterprise_user_isDisabled']">
								<el-option v-for="o in list_user_enterprise_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','enterprise_user')" id="enterprise_user" v-model="form['enterprise_user']" :disabled="true">
								<el-option v-for="o in list_user_enterprise_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="enterprise_user" v-model="form['enterprise_user']" :disabled="disabledObj['enterprise_user_isDisabled']">
							<el-option v-for="o in list_user_enterprise_user" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','name_of_enterprise') || $check_field('add','name_of_enterprise') || $check_field('set','name_of_enterprise')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="企业名称" prop="name_of_enterprise">
												<el-input id="name_of_enterprise" v-model="form['name_of_enterprise']" placeholder="请输入企业名称"
							  v-if="(form['online_recruitment_id'] && $check_field('set','name_of_enterprise')) || (!form['online_recruitment_id'] && $check_field('add','name_of_enterprise'))" :disabled="disabledObj['name_of_enterprise_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','name_of_enterprise')">{{form['name_of_enterprise']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','type_of_enterprise') || $check_field('add','type_of_enterprise') || $check_field('set','type_of_enterprise')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="企业类型" prop="type_of_enterprise">
												<el-input id="type_of_enterprise" v-model="form['type_of_enterprise']" placeholder="请输入企业类型"
							  v-if="(form['online_recruitment_id'] && $check_field('set','type_of_enterprise')) || (!form['online_recruitment_id'] && $check_field('add','type_of_enterprise'))" :disabled="disabledObj['type_of_enterprise_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','type_of_enterprise')">{{form['type_of_enterprise']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','enterprise_address') || $check_field('add','enterprise_address') || $check_field('set','enterprise_address')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="企业地址" prop="enterprise_address">
												<el-input id="enterprise_address" v-model="form['enterprise_address']" placeholder="请输入企业地址"
							  v-if="(form['online_recruitment_id'] && $check_field('set','enterprise_address')) || (!form['online_recruitment_id'] && $check_field('add','enterprise_address'))" :disabled="disabledObj['enterprise_address_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','enterprise_address')">{{form['enterprise_address']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','title_of_recruitment') || $check_field('add','title_of_recruitment') || $check_field('set','title_of_recruitment')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="招聘标题" prop="title_of_recruitment">
												<el-input id="title_of_recruitment" v-model="form['title_of_recruitment']" placeholder="请输入招聘标题"
							  v-if="(form['online_recruitment_id'] && $check_field('set','title_of_recruitment')) || (!form['online_recruitment_id'] && $check_field('add','title_of_recruitment'))" :disabled="disabledObj['title_of_recruitment_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','title_of_recruitment')">{{form['title_of_recruitment']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','post_classification') || $check_field('add','post_classification') || $check_field('set','post_classification')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="岗位分类" prop="post_classification">
								<el-select id="post_classification" v-model="form['post_classification']"						v-if="(form['online_recruitment_id'] && $check_field('set','post_classification')) || (!form['online_recruitment_id'] && $check_field('add','post_classification'))">
						<el-option v-for="o in list_post_classification" :key="o['post_classification']" :label="o['post_classification']"
							:value="o['post_classification']">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','post_classification')">{{form['post_classification']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recruitment_salary') || $check_field('add','recruitment_salary') || $check_field('set','recruitment_salary')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="招聘薪资" prop="recruitment_salary">
												<el-input id="recruitment_salary" v-model="form['recruitment_salary']" placeholder="请输入招聘薪资"
							  v-if="(form['online_recruitment_id'] && $check_field('set','recruitment_salary')) || (!form['online_recruitment_id'] && $check_field('add','recruitment_salary'))" :disabled="disabledObj['recruitment_salary_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','recruitment_salary')">{{form['recruitment_salary']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','deadline') || $check_field('add','deadline') || $check_field('set','deadline')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="截止日期" prop="deadline">
								<el-date-picker :disabled="disabledObj['deadline_isDisabled']" v-if="(form['online_recruitment_id'] && $check_field('set','deadline')) || (!form['online_recruitment_id'] && $check_field('add','deadline'))" id="deadline"
						v-model="form['deadline']" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
					</el-date-picker>
					<div v-else-if="$check_field('get','deadline')">{{form['deadline']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','contact_information') || $check_field('add','contact_information') || $check_field('set','contact_information')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="联系方式" prop="contact_information">
												<el-input id="contact_information" v-model="form['contact_information']" placeholder="请输入联系方式"
							  v-if="(form['online_recruitment_id'] && $check_field('set','contact_information')) || (!form['online_recruitment_id'] && $check_field('add','contact_information'))" :disabled="disabledObj['contact_information_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','contact_information')">{{form['contact_information']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recruitment_requirements') || $check_field('add','recruitment_requirements') || $check_field('set','recruitment_requirements')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="招聘要求" prop="recruitment_requirements">
												<el-input id="recruitment_requirements" v-model="form['recruitment_requirements']" placeholder="请输入招聘要求"
							  v-if="(form['online_recruitment_id'] && $check_field('set','recruitment_requirements')) || (!form['online_recruitment_id'] && $check_field('add','recruitment_requirements'))" :disabled="disabledObj['recruitment_requirements_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','recruitment_requirements')">{{form['recruitment_requirements']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','cover_image') || $check_field('add','cover_image') || $check_field('set','cover_image')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="封面图片" prop="cover_image">
								<el-upload :disabled="disabledObj['cover_image_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_cover_image"
						:show-file-list="false" v-if="(form['online_recruitment_id'] && $check_field('set','cover_image')) || (!form['online_recruitment_id'] && $check_field('add','cover_image'))">
						<img id="cover_image" v-if="form['cover_image']" :src="$fullUrl(form['cover_image'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','cover_image')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['cover_image'])" :preview-src-list="[$fullUrl(form['cover_image'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','recruitment_details') || $check_field('add','recruitment_details') || $check_field('set','recruitment_details')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
				<el-form-item label="招聘详情" prop="recruitment_details">
					<quill-editor v-model.number="form['recruitment_details']"
						v-if="(form['online_recruitment_id'] && $check_field('set','recruitment_details')) || (!form['online_recruitment_id'] && $check_field('add','recruitment_details')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','recruitment_details')" v-html="form['recruitment_details']"></div>
				</el-form-item>
			</el-col>
						
	
	
		
		
						<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 1 > 0">
			  <el-form-item label="投递简历限次">
				<el-input id="limit_times" v-model="form['resume_delivery_limit_times']" placeholder="投递简历限制次数，0为不限"
						  v-if="$check_option('/online_recruitment/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/online_recruitment/view','get')" v-html="form['resume_delivery_limit_times']"></div>
			  </el-form-item>
			</el-col>
			
	
	
	
	</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/online_recruitment/view','set') || $check_action('/online_recruitment/view','add')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "online_recruitment_id",
				url_add: "~/api/online_recruitment/add?",
				url_set: "~/api/online_recruitment/set?",
				url_get_obj: "~/api/online_recruitment/get_obj?",
				url_upload: "~/api/online_recruitment/upload?",

				query: {
					"online_recruitment_id": 0,
				},

				form: {
								"enterprise_user": 0, // 企业用户
										"name_of_enterprise":  '', // 企业名称
										"type_of_enterprise":  '', // 企业类型
										"enterprise_address":  '', // 企业地址
										"title_of_recruitment":  '', // 招聘标题
										"post_classification":  '', // 岗位分类
										"recruitment_salary":  '', // 招聘薪资
										"deadline":  '', // 截止日期
										"contact_information":  '', // 联系方式
										"recruitment_requirements":  '', // 招聘要求
										"cover_image":  '', // 封面图片
										"recruitment_details":  '', // 招聘详情
											"online_recruitment_id": 0, // ID
													"resume_delivery_limit_times": 1, // 投递简历限制次数
										},
				disabledObj:{
								"enterprise_user_isDisabled": false,
										"name_of_enterprise_isDisabled": false,
										"type_of_enterprise_isDisabled": false,
										"enterprise_address_isDisabled": false,
										"title_of_recruitment_isDisabled": false,
										"post_classification_isDisabled": false,
										"recruitment_salary_isDisabled": false,
										"deadline_isDisabled": false,
										"contact_information_isDisabled": false,
										"recruitment_requirements_isDisabled": false,
										"cover_image_isDisabled": false,
										"recruitment_details_isDisabled": false,
										},

	
					// 用户列表
				list_user_enterprise_user: [],
						// 用户组
				group_user_enterprise_user: "",
						
				
				
				
										// 岗位分类选项列表
				list_post_classification: [""],
	
				
				
				
				
				
				
			
			}
		},
		methods: {

	
	
				/**
			 * 获取企业用户用户列表
			 */
			async get_list_user_enterprise_user() {
                var json = await this.$get("~/api/user/get_list?user_group=企业用户");
                if(json.result && json.result.list){
                    this.list_user_enterprise_user = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取企业用户用户组
			 */
			async get_group_user_enterprise_user() {
							this.form["enterprise_user"] = this.$store.state.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=企业用户");
				if(json.result && json.result.obj){
					this.group_user_enterprise_user = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_enterprise_user(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_enterprise_user.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["enterprise_user"] = id
									_this.disabledObj['enterprise_user' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "enterprise_user") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_enterprise_user(id){
				var obj = this.list_user_enterprise_user.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
			
	
			
				/**
			 * 获取岗位分类列表
			 */
			async get_list_post_classification() {
				var json = await this.$get("~/api/post_classification/get_list?");
				if(json.result && json.result.list){
					this.list_post_classification = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
					
			
	
			
	
			
	
			
	
						/**
			 * 上传封面图片
			 * @param {Object} param 图片参数
			 */
			upload_cover_image(param){
									this.uploadFile(param.file, "cover_image");
								},
	
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																					if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																        if (this.form["deadline"] && JSON.stringify(this.form["deadline"]).indexOf("-")===-1){
          this.form["deadline"] = this.$toTime(parseInt(this.form["deadline"]),"yyyy-MM-dd")
        }
													$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){

																																									if(json.result.obj["deadline"]=="0000-00-00"){
				  json.result.obj["deadline"] = null;
				}
				if(parseInt(json.result.obj["deadline"]) > 9999){
					json.result.obj["deadline"] = this.$toTime(parseInt(json.result.obj["deadline"]),"yyyy-MM-dd")
				}
																								

			},

																																																																																										async submit(param, func){
				if (!param) {
					param = this.form;
				}
								var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																																																															ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
																																																																																								if (!param.deadline){
					return "截止日期不能为空";
				}
																																																												return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/online_recruitment/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/online_recruitment/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/online_recruitment/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/online_recruitment/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/online_recruitment/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
					this.get_list_user_enterprise_user();
					this.get_group_user_enterprise_user();
															this.get_list_post_classification();
																},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




</style>
